package com.hsh.maoduo.service.impl;

import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.constant.YTZGPathConstants;
import com.hsh.maoduo.constant.common.DateConstants;
import com.hsh.maoduo.pojo.other.yt.YTZGExport;
import com.hsh.maoduo.service.OrderGoodsService;
import com.hsh.maoduo.service.YTZGService;
import com.hsh.maoduo.util.POIExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class YTZGServiceImpl implements YTZGService {

    @Resource
    private OrderGoodsService orderGoodsService;

    @Override
    public Result writeSendExcel(String dateStr) {
        Date date = null;
        try {
            date = DateConstants.PURE_DATE_FORMAT.parse(dateStr);
        } catch (ParseException e) {
            return Result.success(writeSendExcel());
        }
        return Result.success(writeSendExcel(date));
    }


    private String writeSendExcel() {
        return writeSendExcel(new Date());
    }


    private String writeSendExcel(Date date) {
        String dateStr = DateConstants.PURE_DATE_FORMAT.format(date);
        //读取韵达导出文件
        List<YTZGExport> YTZGExportList = POIExcelUtil.reads(YTZGPathConstants.PDD_EXPORT_PATH_PRE
                , YTZGPathConstants.PDD_EXPORT_NAME_PRE + dateStr
                , YTZGExport.class);
        YTZGExportList.stream().forEach(YTZGExport::init);
        //将读取的数据转成order并写入数据库
        int insertNum = orderGoodsService.insertByExport(YTZGExportList);

        return "新增订单数量：" + insertNum ;
    }
}
